package springboot1706.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import springboot1706.bean.User;
import springboot1706.service.TestService;
import springboot1706.service.UserServiceImpl;
import springboot1706.util.RedisUtil;

import com.google.code.kaptcha.impl.DefaultKaptcha;

@Controller
public class TestController {
	Logger log = LoggerFactory.getLogger(TestController.class);
	@Resource
	private RedisUtil redisUtil;
	@Resource
	private TestService testService;
	@Resource
	private DefaultKaptcha captchaProducer;
	@Autowired
	private UserServiceImpl userServiceImpl;
	
	/**
	 * 主页
	 */



    /**
     * 获取验证码 的 请求路径
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/defaultKaptcha")  
    public void defaultKaptcha(HttpServletRequest request,HttpServletResponse response) throws Exception{  
            byte[] captchaChallengeAsJpeg = null;    
             ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();    
             try {    
             //生产验证码字符串并保存到session中  
             String createText = captchaProducer.createText();  
             request.getSession().setAttribute("vrifyCode", createText);  
             //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中  
             BufferedImage challenge = captchaProducer.createImage(createText);  
             ImageIO.write(challenge, "jpg", jpegOutputStream);  
             } catch (IllegalArgumentException e) {    
                 response.sendError(HttpServletResponse.SC_NOT_FOUND);    
                 return;    
             }   

             //定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组  
             captchaChallengeAsJpeg = jpegOutputStream.toByteArray();    
             response.setHeader("Cache-Control", "no-store");    
             response.setHeader("Pragma", "no-cache");    
             response.setDateHeader("Expires", 0);    
             response.setContentType("image/jpeg");    
             ServletOutputStream responseOutputStream =    
                     response.getOutputStream();    
             responseOutputStream.write(captchaChallengeAsJpeg);    
             responseOutputStream.flush();    
             responseOutputStream.close();    
    }

   

	@RequestMapping("test")
	@ResponseBody
	public String test() {
		return "test=" + testService.getTest(1);
	}
	@RequestMapping("test2")
	public String test2(String username, Model model) {
		model.addAttribute("username", username);
		if("root".equals(username))
			throw new RuntimeException("测试异常");

		return "test2";
	}
	@RequestMapping("test3")
	public String test3(Model model) {
		String key = "key";
		String val = "root";
		if(redisUtil.exists(key)) {
			model.addAttribute("username", redisUtil.get(key));
		} else {
			redisUtil.set(key, val);
		}
		return "test2";
	}	
}
